﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Castle.ActiveRecord;
using Castle.ActiveRecord.Framework;
using System.Reflection;

namespace ZDSoft.LMS.Test
{
    public class TestBase
    {
        public TestBase()
        {
            try
            {
                if (!ActiveRecordStarter.IsInitialized)
                {
                    IConfigurationSource source = System.Configuration.ConfigurationSettings.GetConfig("activerecord") as IConfigurationSource;
                    ActiveRecordStarter.Initialize(typeof(ZDSoft.LMS.Domain.User).Assembly, source);
                }
                ActiveRecordStarter.CreateSchema();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public IList<T> GetEntities<T>(int count)
            where T:class
        {
            IList<T> list = new List<T>();
            T t = null;
            for (int i = 0; i < count; i++)
            {
                t = GetInstance<T>();
                if (t == null) { throw new Exception("无法实例化类型：" + typeof(T).Name); }
                t = GetEntity(t, i);
                list.Add(t);
            }
            return list;
        }

        public T GetEntity<T>(T t, int counter)
            where T:class
        {
            foreach (PropertyInfo info in t.GetType().GetProperties())
            {
                if (info.CanWrite)
                {
                    string type = info.PropertyType.Name.ToLower();
                    if (type == "string")
                    {
                        info.SetValue(t, "t" + counter.ToString(), null);  
                    }
                    else if (type == "int")
                    {
                        info.SetValue(t, counter, null);
                    }
                    else if (type == "datetime")
                    {
                        info.SetValue(t, DateTime.Now, null);
                    }
                }
            }
            return t;
        }

        public T GetEntity<T>()
            where T : class
        {
            T t = GetInstance<T>();
            foreach (PropertyInfo info in t.GetType().GetProperties())
            {
                if (info.CanWrite)
                {
                    string type = info.PropertyType.Name.ToLower();
                    if (type == "string")
                    {
                        info.SetValue(t, "t", null);
                    }
                    else if (type == "int")
                    {
                        info.SetValue(t, 1, null);
                    }
                    else if (type == "datetime")
                    {
                        info.SetValue(t, DateTime.Now, null);
                    }
                }
            }
            return t;
        }

        private T GetInstance<T>()
            where T : class
        {
            return typeof(T).GetConstructor(Type.EmptyTypes).Invoke(null) as T;
        }
    }
}
